<template>
  <div class="page_box">
    <el-input v-model="iptValue" placeholder="请输入内容" />

    <button class="btn" ref="copybtn">复制</button>
  </div>
</template>

<script>
import ClipboardJS from "clipboard";
export default {
  name: "clipboardPage",
  data() {
    return {
      clipboard: null,
      iptValue: "文本",
    };
  },
  mounted() {
    // 这里使用的是函数形式
    this.clipboard = new ClipboardJS(this.$refs.copybtn, {
      text: () => {
        return this.iptValue; // 这里返回任意的想要 粘贴的数据
      },
    });
    this.clipboard.on("success", (e) => {
      console.log("复制成功", e.text);
    });
    // 注册事件
    this.clipboard.on("error", (e) => {
      console.log("复制失败", e);
    });
  },
  beforeDestroy() {
    this.clipboard?.destroy();
  },
};
</script>

<style></style>
